//
//  BHMediaTool.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/24.
//

import UIKit
import Photos

enum BH_ALBUM_SOURCE {
    case image, video
}

class BHMediaTool {
    
    static func sourceFromAlbum(type:BH_ALBUM_SOURCE, videoMaxTime: TimeInterval, completeHandler: @escaping ([BHImageOrVideoModel]) -> Void) {
        
        var tempArray: [BHImageOrVideoModel] = []
        
        //  获取所有系统图片信息集合体
        let allOptions = PHFetchOptions()
        //  对内部元素排序，按照时间由远到近排序
        allOptions.sortDescriptors = [NSSortDescriptor.init(key: "creationDate", ascending: true)]
        //  将元素集合拆解开，此时 allResults 内部是一个个的PHAsset单元
        let allResults = PHAsset.fetchAssets(with: allOptions)
        allResults.enumerateObjects { (asset, index, finished) in
            if type == .image {
                if asset.mediaType == .image {
                    let model: BHImageOrVideoModel = BHImageOrVideoModel.init()
                    model.sourceType = 0
                    model.asset = asset
                    tempArray.insert(model, at: 0)
                }
            }
            if type == .video {
                if asset.mediaType == .video {
                    if videoMaxTime == 0 {
                        let model: BHImageOrVideoModel = BHImageOrVideoModel.init()
                        model.asset = asset
                        model.sourceType = 1
                        tempArray.insert(model, at: 0)
                    }else {
                        if asset.duration <= videoMaxTime {
                            let model: BHImageOrVideoModel = BHImageOrVideoModel.init()
                            model.asset = asset
                            model.sourceType = 1
                            tempArray.insert(model, at: 0)
                        }
                    }
                }
            }
        }
        completeHandler(tempArray)
    }
    
    
    static func showPreview(array: [BHImageOrVideoModel], current: Int?) {
        if array.count == 0 {
            return
        }
        let previewVC = BHImageOrVideoPreviewViewController.init()
        let nav: BHNavigationController = BHNavigationController.init(rootViewController: previewVC)
        
        var temp: [BHImageOrVideoModel] = []
        for model in array {
            if model.sourceType == 0 {
                temp.append(model)
            }
        }
        previewVC.sourceArray = temp
        if current != nil {
            if current! >= temp.count {
                previewVC.index = 0
            }else {
                previewVC.index = current!
            }
        }
        BHNavigationController.currentNavigationController?.present(nav, animated: true, completion: nil)
    }
    
    static func previewVideo(model: BHImageOrVideoModel) {
        let previewVC = BHImageOrVideoPreviewViewController.init()
        let nav: BHNavigationController = BHNavigationController.init(rootViewController: previewVC)
        previewVC.avModel = model
        BHNavigationController.currentNavigationController?.present(nav, animated: true, completion: nil)
    }
}
